package com.lpf.bytebuf;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author LIUPEIFENG
 * @date 2019-12-10 12:40
 */
public class AtomicUpdaterTest {

    public static void main(String[] args) {
        Person person = new Person();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(() -> {
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(person.age++);
            });

            thread.start();
        }



        //Person person = new Person();
        //AtomicIntegerFieldUpdater<Person> updater = AtomicIntegerFieldUpdater.newUpdater(Person.class, "age");
        //for (int i = 0; i < 10; i++) {
        //    Thread thread = new Thread(() -> {
        //        try {
        //            Thread.sleep(20);
        //        } catch (InterruptedException e) {
        //            e.printStackTrace();
        //        }
        //        System.out.println(updater.getAndIncrement(person));
        //    });
        //
        //    thread.start();
        //}

    }
}


class Person {
    volatile int age = 1;
}